import sys
import os
from database import DataBase
from student import Student


# 格式化输出Student数组
def print_student_info(student_list):
    if isinstance(student_list, list):
        if len(student_list) == 0:
            print('''
=========================
    没有符合条件的学生信息
=========================
            ''')
        else:
            print("{:<12}\t{:<12}\t{:<12}\t{:<12}\t{:<12}\t{:<12}".format(
                "学号", "姓名",
                "English成绩",
                "python成绩",
                "java成绩",
                "总成绩"))
            for i in student_list:
                print("{:<12}\t{:<12}\t{:<12g}\t{:<12g}\t{:<12g}\t{:<12g}".format(
                    i.stuid,
                    i.name,
                    i.english_grade,
                    i.python_grade,
                    i.java_grade,
                    i.total_grade))
    else:
        print("没有符合条件的学生信息")

# 添加学生
def add_student():
    stuid = input("请输入学号：")
    name = input("请输入姓名：")
    english_grade = input("请输入英语成绩：")
    python_grade = input("请输入python成绩：")
    java_grade = input("请输入java成绩：")

    try:
        student = Student(stuid, name, english_grade, python_grade, java_grade)
        DataBase.add_student_info(student)
        # DataBase.add_student_info(student)
    except Exception as e:
        print("输入内容不合法", e)

    isContinue = input("是否继续添加学生y/n(其他任意键)")
    if isContinue is 'y':
        add_student()

# 查找学生
def query_student():
    print('''
=====================================
            1.学号查询
            2.姓名查询
            0.返回菜单(其他任意键)
=====================================
    ''')
    num = input("请输入选择：：")
    if num == '1':
        stuid = input("请输入学号：")
        student_list = DataBase.query_student_info_by_stuid(stuid)
        print_student_info(student_list)
    elif num == '2':
        name = input("请输入姓名：")
        student_list = DataBase.query_student_info_by_name(name)
        print_student_info(student_list)
    else:
        pass
    os.system("pause")

# 删除学生
def delete_student():
    stuid = input("请输入学号：")
    student_list = DataBase.query_student_info_by_stuid(stuid)
    print_student_info(student_list)
    if len(student_list) != 0:
        num = input("是否删除该学生y/n(其他任意键)")
        if num == 'y':
            DataBase.delete_student_info_by_stuid(stuid)
            num = input("是否继续删除学生y/n(其他任意键)")
            if num == 'y':
                delete_student()


# 编辑学生
def edit_student():
    stuid = input("请输入学号：")
    student_list = DataBase.query_student_info_by_stuid(stuid)
    print_student_info(student_list)
    if len(student_list) != 0:
        print('''
=====================================
            1.修改改学生信息
            0.退出(其他任意键)
=====================================
                ''')
        num = input("请选择功能")
        if num == '1':
            student = student_list[0]
            student.name = input("请输入修改后的姓名：")
            student.english_grade = input("请输入修改后的English成绩：")
            student.python_grade = input("请输入修改后的python成绩：")
            student.java_grade = input("请输入修改后的java成绩：")
            DataBase.edit_student_info(student)
            num = input("是否继续修改学生信息y/n(其他任意键)")
            if num == 'y':
                edit_student()


# 排序
def sort_student():
    sortType = False
    print('''
    ======================================
                1.升序
                2.降序
                0.返回菜单(其它任意键)
    ======================================
        ''')
    num = input("请选择排序方式：")

    if num == '1':
        sortType = False
    elif num == '2':
        sortType = True
    else:
        return

    print('''
======================================
            1.学号排序
            2.姓名排序
            3.English成绩排序
            4.python成绩排序
            5.java成绩排序
            6.总成绩排序
            0.返回菜单(其它任意键)
======================================
    ''')

    num = input("请选择功能：")
    student_list = DataBase.get_student_list()

    numbers = {
        '1': lambda x: list(x.stuid),
        '2': lambda x: list(x.name),
        '3': lambda x: x.english_grade,
        '4': lambda x: x.python_grade,
        '5': lambda x: x.java_grade,
        '6': lambda x: x.total_grade,
    }

    method = numbers.get(num)
    if method:
        student_list.sort(key=method, reverse=sortType)
        print_student_info(student_list)
    else:
        return

    num = input("继续排序y/n(其它任意键)")

    if num == 'y':
        sort_student()


# 统计
def statistics_student_info():
    student_list = DataBase.get_student_list()
    student_num = len(student_list)
    registered_num = 0
    total_grade = 0.0
    average_grade = 0.0
    for item in student_list:
        if item.english_grade < 60 or item.python_grade < 60 or item.java_grade < 60:
            registered_num = registered_num + 1
        total_grade = total_grade + item.total_grade
    average_grade = total_grade / student_num

    print('''
=======================================================
学生总人数：{}
挂科人数：{}
平均总成绩：{:.2f}
总成绩：{:g}
=======================================================
    '''.format(student_num, registered_num, average_grade, total_grade))

    os.system('pause')

# 展示所有学生信息
def show_all_student_info():
    student_list = DataBase.get_student_list()
    print_student_info(student_list)
    os.system('pause')


def quit_sys():
    num = input("是否确认退出y/n(其它任意键)")
    if num == 'y':
        print('''
    =======================
            退出成功
           欢迎下次使用
    ========================
        ''')
        sys.exit(0)


def input_result(num):
    numbers = {
        '1': add_student,
        '2': query_student,
        '3': delete_student,
        '4': edit_student,
        '5': sort_student,
        '6': statistics_student_info,
        '7': show_all_student_info,
        '0': quit_sys
    }
    method = numbers.get(num)
    if method:
        method()
    else:
        print("输入不合法")
    os.system("cls")


def menu():
    print('''
            学生信息管理系统
======================================
            1.录入学生信息
            2.查找学生信息
            3.删除学生信息
            4.修改学生信息
            5.排序
            6.统计学生信息
            7.显示所有学生信息
            0.退出
======================================
    ''')
    num = input("请选择功能：")
    input_result(num)


if __name__ == "__main__":
    DataBase.init()
    while True:
        menu()
